function aaa(){
	alert(123);
}
$(function(){
	$("select").each(function(){
		$(this).append("<option value=''>---请选择---</option>");
	});

	var basePath = $("#basePath").val();
	$.ajax({
		url: basePath + "base_info/ajax_un_pigeonhole_base_data",
		type: "GET",
		dataType: "json",
		success: function( datas ) {
			console.log(datas);
			init(datas);
			
		}
	});
	
	$("#queryForm input[name='onlyId']").focus();
	
	$("input[name='onlyId']").keydown(function(e){
		if(e.keyCode == 13){
			var onlyId = $(this).val();
			if(onlyId.length > 2){
				queryBtnClick();
				$(this).val(null);
			}
			
		}
	});
});

function init(baseInfo){
	var medicalDepts = baseInfo['medicalDepts'];
	$("select[name='outHospitalDeptCode'],[name='outDeptCode']").each(function(){
		var options = "";
		for(var index in medicalDepts){
			var medicalDept = medicalDepts[index];
	    	options+="<option value='"+medicalDept.uniq_code+"'>"+medicalDept.label+"</option>"
	    } 
       $(this).append(options);
       
	});
	
	$("select[name='status']").each(function(){
		var options = "<option value='0'>---未完成质控---</option>";
		options += "<option value='1'>---已完成质控---</option>";
       $(this).append(options);
	});
}


/*点击查询按钮*/
function queryBtnClick(){
	$("input[name='currentPage']").val(1);
	queryFormSubmit();
}

/*提交查询*/
function queryFormSubmit(){
	var submitData = $('#queryForm').serialize();
	var basePath = $("#basePath").val();
	layer.load(1);
	clearPage();
	$.ajax({
		url: basePath + "medical_record/ajax_query_quality_control",
		dataType: "json",
		data:submitData,
		success: function( data ) {
			var success = data['success'];
			if(success){
				
				var params = data['data'];
				var totalCounts = params['totalCounts'];
				if(totalCounts > 0){
					layer.msg("查询成功",{offset: '350px'});
					var medicalRecords = params['queryDatas'];
					addRows(medicalRecords);
					var pageSize = params['pageSize'];
					var currentPage = params['currentPage'];
					initPage(parseInt(totalCounts),parseInt(pageSize),parseInt(currentPage));
				}else{
					layer.msg("查询结果为空",{offset: '350px'});
				}
				
			}else{
				layer.msg("查询失败");
			}
			console.log(data);
			
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			if(XMLHttpRequest.status == 200){
				if(textStatus == 'parsererror'){
					layer.msg("登陆已过期，请重新登陆");
				}else{
					layer.msg("未知错误，请联系管理员");
				}
			}else if(XMLHttpRequest.status == 0){
				if(textStatus == 'error'){
					layer.msg("无法连接服务器，请检测网络");
				}else{
					layer.msg("未知错误，请联系管理员");
				}
			}else{
				layer.msg("未知错误，请联系管理员");
			}
		},
		complete:function(XMLHttpRequest, textStatus){
			layer.closeAll('loading');
			parent.document.getElementById("mainContent").height=document.body.scrollHeight + 50;
			$("input[name='onlyId']").focus();
		}
	});
	
};

/*添加列表行*/
function addRows(medicalRecords){
	var basePath = $("#basePath").val();
	for(var index in medicalRecords){
		var medicalRecord = medicalRecords[index];
		var add_content=$("#template_tr").clone();
		add_content.removeAttr("hidden");
		add_content.attr("id",medicalRecord.visit_guid);
		add_content.find("span[class='only_id']").html(medicalRecord.only_id);
		add_content.find("span[class='mr_id']").html(medicalRecord.mr_id);
		add_content.find("span[class='patient_name']").html(medicalRecord.patient_name);
		add_content.find("span[class='id_number']").html(medicalRecord.id_number);
		add_content.find("span[class='visit_number']").html(medicalRecord.visit_number);
		add_content.find("span[class='out_dept_name']").html(medicalRecord.out_dept_name);
		add_content.find("span[class='out_hospital_date']").html(medicalRecord.out_hospital_date);
		add_content.find("span[class='out_hospital_type_name']").html(medicalRecord.out_hospital_type_name);
		add_content.find("span[class='score']").html(medicalRecord.score);
		var page_trace_code = medicalRecord.page_trace_code;
		var trace_code = medicalRecord.trace_code;
		if(trace_code && trace_code=='BAZK'){
			add_content.find("a[class='operate_pigeonhole']").click(function(){qualityControlDetailShow(this)});
			
		}else{
			add_content.find("a[class='operate_pigeonhole']").parent().html("未质控");
			
		}
		$("#query_show_table tbody").append(add_content);
    } 
	
}

/*清空列表*/
function clearPage(){
	$("#template_tr").siblings("tr").remove();
	$("#page_plus").html("");
	$("#totalPage").html("0");
    $("#currentPage").html("0/0");
}

/*初始化分页控件*/
function initPage(totalCounts,pageSize,currentPage){
	 var visiblePages = 5;
	 var totalPages =  totalCounts%pageSize==0?(totalCounts/pageSize):(parseInt(totalCounts/pageSize)+1);
	 if(totalCounts < 1){
		$("#page_plus").html("");
		$("#totalPage").html("0");
        $("#currentPage").html("0/0");
	 }else{
		 $('#page_plus').jqPaginator({

	        totalCounts: totalCounts,
	        pageSize:pageSize,
	        visiblePages: visiblePages,
	        currentPage: currentPage,

	        first:'<li class="first"><a href="javascript:void(0);">首页</a></li>',
	        prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
	        next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
	        last: '<li class="last"><a href="javascript:void(0);">末页</a></li>',
	        page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
	        onPageChange: function (num,type) {

	     		if(type !='init'){
	     			$("input[name='currentPage']").val(num);
	     			queryFormSubmit();
				}
	            $("#totalPage").html(totalCounts);
	            $("#currentPage").html(num+"/"+totalPages);
	        }
	    });
	 }
}

function qualityControlDetailShow(obj){
	var clickContent = $(obj).parent().parent();
	var visitGuid =  clickContent.attr("id");
	var basePath = $("#basePath").val();
	console.log(visitGuid);
	//页面层
	var add_content=$("#template_layer_div").clone();
	add_content.find("#template_layer_record").attr("id","layer_record");
	add_content.find("#template_layer_table").attr("id","layer_table");
	add_content.find("#template_layer_tr").attr("id","layer_tr");
	layer.open({
	  type: 1,
	  title:'质控评分明细',
	  skin: 'layui-layer-rim', //加上边框
	  area: ['930px', '450px'], //宽高
	  content: add_content.html(),
	  success: function(layero, index){
		  $("#layer_record #layerOnlyId").val(clickContent.find(".only_id").html());
		  $("#layer_record #layerPatientName").val(clickContent.find(".patient_name").html());
		  $("#layer_record #layerOutHospitalDate").val(clickContent.find(".out_hospital_date").html());
		  $("#layer_record #layerOutDeptName").val(clickContent.find(".out_dept_name").html());
		  $("#layer_record #layerOutDeptName").val(clickContent.find(".out_dept_name").html());
		  $("#layer_record #layerScore").val(clickContent.find(".score").html());
		  $.ajax({
			  	url: basePath + "medical_record/ajax_medical_record_quality_control",
				type: "GET",
				data:{visitGuid:visitGuid},
				success: function( datas ) {
					var success = datas['success'];
					if(success){
						var data =  datas['data'];
						var medicalRecords = data['medicalRecords'];
						var qualityControlItems = data['qualityControlItems'];
						addItemRows(qualityControlItems);
					}else{
						layer.msg("操作错误，请重试！");
					}
					console.log(datas);
					
				},
				error:function(XMLHttpRequest, textStatus, errorThrown){
					layer.msg("未知错误，请联系管理员");
				},
				complete:function(XMLHttpRequest, textStatus){
					layer.closeAll('loading');
				}
			});
		  
		  
	  }
	});
}

function addItemRows(qualityControlItems){
	for(var index in qualityControlItems){
		var qualityControlItem = qualityControlItems[index];
		var add_content=$("#template_layer_tr").clone();
		add_content.removeAttr("hidden");
		add_content.attr("id",qualityControlItem.id);
		/*var projectDesc = qualityControlItem.first_level_name+";"+qualityControlItem.second_level_name+";"+qualityControlItem.third_level_name;
		add_content.find("span[class='project']").html(projectDesc).attr("title",projectDesc);*/
		add_content.find("span[class='project']").html(qualityControlItem.third_level_name).attr("title",qualityControlItem.third_level_name);
		add_content.find("span[class='deduction']").html(qualityControlItem.deduction);
		add_content.find("span[class='remark']").html(qualityControlItem.remark).attr("title",qualityControlItem.remark);
		add_content.find("span[class='last_user_name']").html(qualityControlItem.last_user_name);
		last_date_time_format = qualityControlItem.last_date_time_format;
		if(last_date_time_format){
			add_content.find("span[class='last_date_format']").html(last_date_time_format.substr(0,10)).attr("title",last_date_time_format);
			
		}
		$("#layer_table tbody").append(add_content);
    } 
	
}
